﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.CompilerServices;
using Microsoft.Scripting.Runtime;

namespace Cjc.ExpressionEngine.Excel.Runtime.Operations
{
	public static class Int32Ops
	{
		[ImplicitConversionMethod]
		public static decimal ConvertToDecimal( int x ) { return (decimal)x; }
//		[ImplicitConversionMethod]
//		public static double ConvertToDouble( int x ) { return (double)x; }
		[ImplicitConversionMethod]
		public static string ConvertToString( int x ) { return x.ToString(); }

		[SpecialName]
		public static object Add( int x, int y ) { return Reduce( BigIntOps.Add( x, y ) ); }
		[SpecialName]
		public static object Subtract( int x, int y ) { return Reduce( BigIntOps.Subtract( x, y ) ); }
		[SpecialName]
		public static object Multiply( int x, int y ) { return Reduce( BigIntOps.Multiply( x, y ) ); }
		[SpecialName]
		public static object Divide( int x, int y ) { return Reduce( BigIntOps.Divide( x, y ) ); }

		public static object Reduce( object value )
		{
			if ( value != null && value.GetType() == typeof( long ) )
			{
				var l = (long)value;

				if ( l >= int.MinValue && l <= int.MaxValue ) return (int)l;
				return l;
			}

			return value;
		}
	}
}